package com.xbongbong.saas.model.impl;

import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.constant.ParameterConstant;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.domain.dao.TransferProductDao;
import com.xbongbong.saas.domain.entity.BusinessProductEntity;
import com.xbongbong.saas.domain.entity.TransferProductEntity;
import com.xbongbong.saas.model.TransferProductModel;
import com.xbongbong.saas.util.StandardizeDataUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author feng.zheng
 * @version v1.0
 * @date 2019/2/19 14:52
 * @since v1.0
 */
@Service("transferProductModel")
public class TransferProductModelImpl implements TransferProductModel {

    @Resource
    private TransferProductDao transferProductDao;
    /**
     * 调拨单产品数据插入
     *
     * @param entity 调拨单产品实体
     * @return Integer
     * @throws XbbException 异常
     * @author feng.zheng
     * @date 2019-02-01 15:16
     */
    @Override
    public Integer insert(TransferProductEntity entity) throws XbbException {
        StandardizeDataUtil.standardizeNotString(entity);
        return transferProductDao.insert(entity);
    }

    /**
     * 调拨单产品数据更新
     *
     * @param entity 调拨单产品实体
     * @return Integer
     * @throws XbbException 异常
     * @author feng.zheng
     * @date 2019-02-01 15:17
     */
    @Override
    public Integer update(TransferProductEntity entity) throws XbbException {
        StandardizeDataUtil.standardizeNotString(entity);
        entity.setUpdateTime(DateTimeUtil.getInt());
        return transferProductDao.update(entity);
    }

    /**
     * 调拨单产品数据查询多个
     *
     * @param param 参数
     * @return List<TransferProductEntity>
     * @author feng.zheng
     * @date 2019-02-01 15:18
     */
    @Override
    public List<TransferProductEntity> findEntitys(Map<String, Object> param) {
        return transferProductDao.findEntitys(param);
    }

    /**
     * 调拨单产品数据查询条数
     *
     * @param param 参数
     * @return java.lang.Integer
     * @author feng.zheng
     * @date 2019-02-01 15:18
     */
    @Override
    public Integer getEntitysCount(Map<String, Object> param) {
        return transferProductDao.getEntitysCount(param);
    }

    @Override
    public List findEntitysWithoutSub(Map<String, Object> param) {
        return null;
    }

    /**
     * 调拨单产品数据单条查询
     *
     * @param key    主键ID
     * @param corpid 公司ID
     * @return java.lang.Integer
     * @author feng.zheng
     * @date 2019-02-01 15:19
     */
    @Override
    public TransferProductEntity getByKey(Long key, String corpid) {
        return transferProductDao.getByKey(key, corpid);
    }

    /**
     * 根据主键删除调拨单产品
     *
     * @param key    主键
     * @param corpid 公司id
     * @return java.lang.Integer
     * @author feng.zheng
     * @date 2019/2/19 14:49
     */
    @Override
    public Integer deleteByKey(Long key, String corpid) {
        return transferProductDao.deleteByKey(key, corpid);
    }

    /**
     * 根据调拨单id删除
     *
     * @param transferId 调拨单id
     * @param corpid     公司id
     * @return java.lang.Integer
     * @author feng.zheng
     * @date 2019/2/19 14:51
     */
    @Override
    public Integer deleteByTransferId(Long transferId, String corpid) {
        return transferProductDao.deleteByTransferId(transferId, corpid);
    }

    @Override
    public List<TransferProductEntity> findEntitysJoinProduct(Map<String, Object> param) {
        return transferProductDao.findEntitysJoinProduct(param);
    }

    /**
     * 产品名称或编号通过产品名称或编号查询对应的调拨单id
     *
     * @param corpid       公司id
     * @param nameOrNoLike 产品名称或编号通过产品名称或编号查询对应的调拨单id
     * @return TransferIds
     */
    @Override
    public List<Long> findTransferIdsByRelaProNameOrNo(String corpid, String nameOrNoLike) {
        Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put("corpid", corpid);
        param.put("nameOrNoLike", nameOrNoLike);
        param.put("del", DelEnum.NORMAL.getDel());
        return transferProductDao.findTransferIdsByRelaProNameOrNo(param);
    }

    /**
     * 获取调拨单关联产品
     *
     * @param id     id
     * @param corpid 公司id
     * @return 产品
     */
    @Override
    public List<TransferProductEntity> getProductsByTransferId(Long id, String corpid) {
        // 获取关联产品
        Map<String, Object> modelMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        modelMap.put(ParameterConstant.CORPID, corpid);
        modelMap.put("transferId", id);
        modelMap.put(ParameterConstant.DEL, DelEnum.NORMAL.getDel());
        modelMap.put(ParameterConstant.ORDER_BY_STR, StringConstant.SAAS_LINK_BUSINESS_ID);
        return findEntitys(modelMap);
    }

    @Override
    public List<TransferProductEntity> getProductsByTransferId(Long id, String corpid, List<Integer> delIn) {
        // 获取关联产品
        Map<String, Object> modelMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        modelMap.put(ParameterConstant.CORPID, corpid);
        modelMap.put("transferId", id);
        modelMap.put("delIn", delIn);
        modelMap.put(ParameterConstant.ORDER_BY_STR, BasicConstant.SORT);
        return findEntitys(modelMap);
    }

    @Override
    public Integer deleteBatch(List<Long> idList, String corpid, Integer del) {
        if (CollectionUtils.isEmpty(idList)) {
            return 0;
        }
        return transferProductDao.deleteBatch(idList, corpid, del);
    }

    @Override
    public List<BusinessProductEntity> findProductNum(Map<String, Object> param) {
        return transferProductDao.findProductNum(param);
    }

    @Override
    public Integer updateBatch(List<TransferProductEntity> list, String corpid) {
        if (CollectionUtils.isEmpty(list)) {
            return 0;
        }
        StandardizeDataUtil.standardize4TransferProductEntityList(list);
        return transferProductDao.updateBatch(list, corpid);
    }

    @Override
    public void insertBatch(List<TransferProductEntity> addTransferProductEntityList) {
        if (CollectionUtils.isEmpty(addTransferProductEntityList)) {
            return;
        }
        StandardizeDataUtil.standardize4TransferProductEntityList(addTransferProductEntityList);
        transferProductDao.insertBatch(addTransferProductEntityList);
    }

    @Override
    public Integer updateOutstock(List<TransferProductEntity> list, String corpid) {
        if (CollectionUtils.isEmpty(list)) {
            return 0;
        }
        long now = DateTimeUtil.getInt();
        for (TransferProductEntity entity : list) {
            entity.setUpdateTime(now);
        }
        return transferProductDao.updateOutstock(list, corpid);
    }

    @Override
    public Integer updateInstock(List<TransferProductEntity> list, String corpid) {
        if (CollectionUtils.isEmpty(list)) {
            return 0;
        }
        long now = DateTimeUtil.getInt();
        for (TransferProductEntity entity : list) {
            entity.setUpdateTime(now);
        }
        return transferProductDao.updateInstock(list, corpid);
    }

    @Override
    public List<String> getCorpids() {
        return transferProductDao.getCorpids();
    }

    @Override
    public Integer findProductNumCount(Map<String, Object> param) {
        return transferProductDao.findProductNumCount(param);
    }

    @Override
    public Long isProductSubAttrExist(String attr, String corpid, Integer isArray) {
        return transferProductDao.isProductSubAttrExist(attr, corpid, isArray);
    }

    @Override
    public Double summaryByAttr(String field,String corpid,  Long refId, List<Long> productIdIn){
        return transferProductDao.summaryByAttr(field, corpid, refId, productIdIn);
    }

    @Override
    public Double summaryByAttrFromOutSide(String field,String corpid,  Long refId, List<Long> productIdIn) {
        return transferProductDao.summaryByAttrFromOutSide(field, corpid, refId, productIdIn);
    }

    @Override
    public List<TransferProductEntity> getProductsByTransferIdIn(String corpid, List<Long> refIdIn) {
        return transferProductDao.getProductsByTransferIdIn(corpid, refIdIn);
    }

    @Override
    public Double calcuateByAttrFromOutSide(String field1, String field2, String corpid, Long refId, List<Long> productIdIn) {
        return transferProductDao.calcuateByAttrFromOutSide(field1, field2, corpid, refId , productIdIn);
    }

    @Override
    public Long countProductSub(String attr, String corpid, Integer isArray) {
        return transferProductDao.countProductSub(attr, corpid, isArray);
    }

    @Override
    public List<TransferProductEntity> getProductWithSubData(String attr, String corpid, Integer isArray, Long idGte, Integer limit) {
        return transferProductDao.getProductWithSubData(attr, corpid, isArray, idGte, limit);
    }

    @Override
    public void deleteByTransferIds(List<Long> transferIds, String corpid) {
        if (CollectionUtils.isEmpty(transferIds)) {
            return;
        }
        transferProductDao.deleteByTransferIdIn(transferIds, corpid);
    }

    @Override
    public Integer updateMulti(List<TransferProductEntity> list, String corpid) {
        if (CollectionUtils.isEmpty(list)) {
            return 0;
        }
        StandardizeDataUtil.standardize4TransferProductEntityList(list);
        return transferProductDao.updateMulti(list, corpid);
    }
}
